# 初始化
import os

import django

# 在environ字典里设置默认Django环境，'xxxx.settings'指Django项目的配置文件
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')
django.setup()

from dvadmin.system.models import MenuButton, Menu, Role
from dvadmin.utils.core_initialize import CoreInitialize


class Initialize(CoreInitialize):

    def init_menu(self):
        """
        初始化菜单表
        """
        self.menu_data = [
            {"id": "c46b4610-abb2-42d4-9894-e0c721922213", "name": "租户管理", "sort": 0, "web_path": "",
             "icon": "truck", "parent_id": None},
            {"id": "2993f83a-8568-4f7e-8e64-efc67b155117", "name": "租户信息", "sort": 1, "web_path": "/tenantClient",
             "icon": "building", "parent_id": "c46b4610-abb2-42d4-9894-e0c721922213",
             "component": "dvadmin_plugins/dvadmin_tenant_web/client", "component_name": "tenantClient"},
            {"id": "de061d11-7b02-410d-b043-5bf982a1eead", "name": "租户domain", "sort": 2, "web_path": "/tenantDomain",
             "icon": "shopping-basket", "parent_id": "c46b4610-abb2-42d4-9894-e0c721922213", "visible": 0,
             "component": "dvadmin_plugins/dvadmin_tenant_web/domain", "component_name": "tenantDomain"},
        ]
        self.save(Menu, self.menu_data, "菜单表")

    def init_menu_button(self):
        """
        初始化菜单权限表
        """
        self.menu_button_data = [
            {'id': '8e09c3ac-6f74-4d1f-af41-4a55456e34b3', 'menu_id': '2993f83a-8568-4f7e-8e64-efc67b155117',
             'name': '查询', 'value': 'Search', 'api': '/api/api/tenant/client/', 'method': 0},
            {'id': '39427085-acf7-41a9-a86d-9604ed098307', 'menu_id': '2993f83a-8568-4f7e-8e64-efc67b155117',
             'name': '新增', 'value': 'Create', 'api': '/api/api/tenant/client/', 'method': 1},
            {'id': '5cfc0715-72a0-4a73-8cc1-03fcb70116c7', 'menu_id': '2993f83a-8568-4f7e-8e64-efc67b155117',
             'name': '编辑', 'value': 'Update', 'api': '/api/api/tenant/client/{id}/', 'method': 2},
            {'id': 'd2b29116-48bc-4126-8b0f-e625a27db66c', 'menu_id': '2993f83a-8568-4f7e-8e64-efc67b155117',
             'name': '删除', 'value': 'Delete', 'api': '/api/api/tenant/client/{id}/', 'method': 3},
            {'id': 'eb1f38b1-d437-44bf-8de9-8f5b49a574ec', 'menu_id': '2993f83a-8568-4f7e-8e64-efc67b155117',
             'name': '单例', 'value': 'Retrieve', 'api': '/api/api/tenant/client/{id}/', 'method': 0},
            {'id': 'fc42375f-5e32-4664-ab0b-d6a5bb8d0870', 'menu_id': 'de061d11-7b02-410d-b043-5bf982a1eead',
             'name': '查询', 'value': 'Search', 'api': '/api/api/tenant/domain/', 'method': 0},
            {'id': 'd6cd7148-a9ca-4271-95c6-dca2d430f091', 'menu_id': 'de061d11-7b02-410d-b043-5bf982a1eead',
             'name': '新增', 'value': 'Create', 'api': '/api/api/tenant/domain/', 'method': 1},
            {'id': '6429fca5-3282-4c97-ad5a-9fc5eddaf819', 'menu_id': 'de061d11-7b02-410d-b043-5bf982a1eead',
             'name': '编辑', 'value': 'Update', 'api': '/api/api/tenant/domain/{id}/', 'method': 2},
            {'id': 'bd079dfa-3800-47cf-a317-674ef880259c', 'menu_id': 'de061d11-7b02-410d-b043-5bf982a1eead',
             'name': '删除', 'value': 'Delete', 'api': '/api/api/tenant/domain/{id}/', 'method': 3},
            {'id': '30150a14-f53b-44d8-b768-3a7d7c19e4c1', 'menu_id': 'de061d11-7b02-410d-b043-5bf982a1eead',
             'name': '单例', 'value': 'Retrieve', 'api': '/api/api/tenant/domain/{id}/', 'method': 0},
        ]

        # value = {"Search": "查询", "Create": "新增", "Update": "编辑", "Delete": "删除", "Retrieve": "单例"}
        # import uuid
        # for menu in self.menu_data:
        #     for index, val in enumerate(value.items()):
        #         api = menu.get("component")
        #         if not api: continue
        #         api = api.strip('/')
        #         new_api = ""
        #         for ele in api:
        #             if ele.isupper():
        #                 new_api += f"_{ele.lower()}"
        #             else:
        #                 new_api += ele
        #         self.menu_button_data.append(
        #             {
        #                 "id": str(uuid.uuid4()),
        #                 "menu_id": menu.get("id"),
        #                 "name": val[1],
        #                 "value": val[0],
        #                 "api": '/api/' + new_api + ('/{id}/' if val[0] in ["Update", "Delete", "Retrieve"] else '/'),
        #                 "method": 0 if index == 4 else index}
        #         )
        # print(1, self.menu_button_data)
        self.save(MenuButton, self.menu_button_data, "菜单权限表")

    def init_role(self):
        """
        初始化角色表
        """
        role = Role.objects.get(id="36001d1a-1b3e-4413-bdfe-b3bc04375f46")
        role.menu.add(*[ele.get("id") for ele in self.menu_data])
        role.permission.add(*[ele.get("id") for ele in self.menu_button_data])

    def run(self):
        self.init_menu()
        self.init_menu_button()
        self.init_role()


def main(reset=False):
    Initialize(reset).run()
    pass


if __name__ == '__main__':
    main()
